/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tenant.annotation.TenantIgnore;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.entity.MaintainCheckContent;
import org.springblade.modules.admin.service.IMaintainCheckContentService;
import org.springblade.modules.admin.vo.MaintainTemplateLocationTypeVO;
import org.springblade.modules.admin.vo.PlanTemplateVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.admin.entity.MaintainCheckHistory;
import org.springblade.modules.admin.vo.MaintainCheckHistoryVO;
import org.springblade.modules.admin.wrapper.MaintainCheckHistoryWrapper;
import org.springblade.modules.admin.service.IMaintainCheckHistoryService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 点检内容历史记录 控制器
 *
 * @author BladeX
 * @since 2021-05-11
 */
@RestController
@AllArgsConstructor
@RequestMapping("admin/maintainCheckHistory")
@Api(value = "点检内容历史记录", tags = "点检内容历史记录接口")
public class MaintainCheckHistoryController extends BladeController {

	private final IMaintainCheckHistoryService maintainCheckHistoryService;

	private final IMaintainCheckContentService maintainCheckContentService;

	/**
	 * 获取任务工作项
	 *
	 * @param planId     工单id
	 * @param templateId 模板id
	 * @param isStore    1:获取评分工作项，不传默认所有
	 * @return 数据集
	 */
	@TenantIgnore
	@GetMapping("/work/item")
	public R<List<PlanTemplateVO>> workItem(Long planId, Long templateId, Integer isStore) {
		return R.data(maintainCheckHistoryService.getPlanWorkItem(planId, templateId, isStore));
	}


	/**
	 * 获取任务状态及关联人员列表
	 *
	 * @param planId
	 * @return
	 */
	@GetMapping("/detail")
	public R<?> planDetail(Long planId) {
		return R.data(maintainCheckHistoryService.selectPlanDetail(planId));
	}

	/**
	 * 分页 点检内容历史记录
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入maintainCheckHistory")
	public R<IPage<MaintainCheckHistoryVO>> list(MaintainCheckHistory maintainCheckHistory, Query query) {
		IPage<MaintainCheckHistory> pages = maintainCheckHistoryService.page(Condition.getPage(query), Condition.getQueryWrapper(maintainCheckHistory));
		return R.data(MaintainCheckHistoryWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 点检内容历史记录
	 */
//	@GetMapping("/page")
//	@ApiOperationSupport(order = 3)
//	@ApiOperation(value = "分页", notes = "传入maintainCheckHistory")
//	public R<IPage<MaintainCheckHistoryVO>> page(MaintainCheckHistoryVO maintainCheckHistory, Query query) {
//		IPage<MaintainCheckHistoryVO> pages = maintainCheckHistoryService.selectMaintainCheckHistoryPage(Condition.getPage(query), maintainCheckHistory);
//		return R.data(pages);
//	}

	/**
	 * 新增 点检内容历史记录
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入maintainCheckHistory")
	public R save(@Valid @RequestBody MaintainCheckHistory maintainCheckHistory) {
		return R.status(maintainCheckHistoryService.save(maintainCheckHistory));
	}

	/**
	 * 修改 点检内容历史记录
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入maintainCheckHistory")
	public R update(@Valid @RequestBody MaintainCheckHistory maintainCheckHistory) {
		return R.status(maintainCheckHistoryService.updateById(maintainCheckHistory));
	}

	/**
	 * 新增或修改 点检内容历史记录
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入maintainCheckHistory")
	public R submit(@Valid @RequestBody MaintainCheckHistory maintainCheckHistory) {
		return R.status(maintainCheckHistoryService.saveOrUpdate(maintainCheckHistory));
	}


	/**
	 * 删除 点检内容历史记录
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(maintainCheckHistoryService.deleteLogic(Func.toLongList(ids)));
	}


	@TenantIgnore
	@PostMapping("/init")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "初始化数据", notes = "")
	public void init() {
		maintainCheckHistoryService.initTemplateSceneType();
	}

//	@GetMapping("/special/type/detail")
//	@ApiOperation(value = "获取专项保养任务列表")
//	@ApiOperationSupport(order = 9)
//	public R<List<MaintainTemplateLocationTypeVO>> specialTypeDetail(Long planId){
//		return R.data(maintainCheckHistoryService.specialTypeDetail(planId));
//	}

	@GetMapping("getWorksImg")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "获取工作项的图片", notes = "传入ids")
	public R<List<HashMap<String, Object>>> getWorksImg(String ids) {
		List<String> list = Arrays.asList(ids.split(","));
		List<MaintainCheckHistory> maintainCheckHistories = maintainCheckHistoryService.listByIds(list);
		List<List<HashMap<String, Object>>> lists = maintainCheckHistories.stream().filter(o -> o.getRequireImg() != null).map(MaintainCheckHistory::getRequireImg).collect(Collectors.toList());
		List<HashMap<String, Object>> resultList = new ArrayList<>(16);
		for (int i = 0; i < lists.size(); i++) {
			resultList.addAll(lists.get(i));
		}
		return R.data(resultList);
	}
}
